package com.hytc.mymall.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.util.AttributeSet;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.widget.FrameLayout;

import com.hytc.mymall.R;
import com.hytc.mymall.utils.BaseTools;

/**
 * Created by Administrator on 2016/12/2.
 */

public class OutlineContainer extends FrameLayout implements Animatable
{
    private static final long ANIMATION_DURATION = 500L;
    private static final long FRAME_DURATION = 16L;
    private float mAlpha;
    private final Interpolator mInterpolator;
    private boolean mIsRunning;
    private Paint mOutlinePaint;
    private long mStartTime;
    private final Runnable mUpdater;

    public OutlineContainer(final Context context) {
        super(context);
        this.mIsRunning = false;
        this.mAlpha = 1.0f;
        this.mInterpolator = (Interpolator)new Interpolator() {
            public float getInterpolation(final float n) {
                final float n2 = n - 1.0f;
                return 1.0f + n2 * (n2 * n2);
            }
        };
        this.mUpdater = new Runnable() {
            @Override
            public void run() {
                final long n = AnimationUtils.currentAnimationTimeMillis() - OutlineContainer.this.mStartTime;
                if (n >= 500L) {
                    OutlineContainer.access$1(OutlineContainer.this, 0.0f);
                    OutlineContainer.this.invalidate();
                    OutlineContainer.this.stop();
                    return;
                }
                OutlineContainer.access$1(OutlineContainer.this, OutlineContainer.this.mInterpolator.getInterpolation(1.0f - n / 500.0f));
                OutlineContainer.this.invalidate();
                OutlineContainer.this.postDelayed(OutlineContainer.this.mUpdater, 16L);
            }
        };
        this.init();
    }

    public OutlineContainer(final Context context, final AttributeSet set) {
        super(context, set);
        this.mIsRunning = false;
        this.mAlpha = 1.0f;
        this.mInterpolator = (Interpolator)new Interpolator() {
            public float getInterpolation(final float n) {
                final float n2 = n - 1.0f;
                return 1.0f + n2 * (n2 * n2);
            }
        };
        this.mUpdater = new Runnable() {
            @Override
            public void run() {
                final long n = AnimationUtils.currentAnimationTimeMillis() - OutlineContainer.this.mStartTime;
                if (n >= 500L) {
                    OutlineContainer.access$1(OutlineContainer.this, 0.0f);
                    OutlineContainer.this.invalidate();
                    OutlineContainer.this.stop();
                    return;
                }
                OutlineContainer.access$1(OutlineContainer.this, OutlineContainer.this.mInterpolator.getInterpolation(1.0f - n / 500.0f));
                OutlineContainer.this.invalidate();
                OutlineContainer.this.postDelayed(OutlineContainer.this.mUpdater, 16L);
            }
        };
        this.init();
    }

    public OutlineContainer(final Context context, final AttributeSet set, final int n) {
        super(context, set, n);
        this.mIsRunning = false;
        this.mAlpha = 1.0f;
        this.mInterpolator = (Interpolator)new Interpolator() {
            public float getInterpolation(final float n) {
                final float n2 = n - 1.0f;
                return 1.0f + n2 * (n2 * n2);
            }
        };
        this.mUpdater = new Runnable() {
            @Override
            public void run() {
                final long n = AnimationUtils.currentAnimationTimeMillis() - OutlineContainer.this.mStartTime;
                if (n >= 500L) {
                    OutlineContainer.access$1(OutlineContainer.this, 0.0f);
                    OutlineContainer.this.invalidate();
                    OutlineContainer.this.stop();
                    return;
                }
                OutlineContainer.access$1(OutlineContainer.this, OutlineContainer.this.mInterpolator.getInterpolation(1.0f - n / 500.0f));
                OutlineContainer.this.invalidate();
                OutlineContainer.this.postDelayed(OutlineContainer.this.mUpdater, 16L);
            }
        };
        this.init();
    }

    static /* synthetic */ void access$1(final OutlineContainer outlineContainer, final float mAlpha) {
        outlineContainer.mAlpha = mAlpha;
    }

    private void init() {
        (this.mOutlinePaint = new Paint()).setAntiAlias(true);
        this.mOutlinePaint.setStrokeWidth((float) BaseTools.dpToPx(this.getResources(), 2));
        this.mOutlinePaint.setColor(this.getResources().getColor(R.color.holo_blue));
        this.mOutlinePaint.setStyle(Paint.Style.STROKE);
        final int dpToPx = BaseTools.dpToPx(this.getResources(), 10);
        this.setPadding(dpToPx, dpToPx, dpToPx, dpToPx);
    }

    protected void dispatchDraw(final Canvas canvas) {
        super.dispatchDraw(canvas);
        final int dpToPx = BaseTools.dpToPx(this.getResources(), 5);
        if (this.mOutlinePaint.getColor() != JazzyViewPager.sOutlineColor) {
            this.mOutlinePaint.setColor(JazzyViewPager.sOutlineColor);
        }
        this.mOutlinePaint.setAlpha((int)(255.0f * this.mAlpha));
        canvas.drawRect(new Rect(dpToPx, dpToPx, this.getMeasuredWidth() - dpToPx, this.getMeasuredHeight() - dpToPx), this.mOutlinePaint);
    }

    public boolean isRunning() {
        return this.mIsRunning;
    }

    public void setOutlineAlpha(final float mAlpha) {
        this.mAlpha = mAlpha;
    }

    public void start() {
        if (this.mIsRunning) {
            return;
        }
        this.mIsRunning = true;
        this.mStartTime = AnimationUtils.currentAnimationTimeMillis();
        this.post(this.mUpdater);
    }

    public void stop() {
        if (!this.mIsRunning) {
            return;
        }
        this.mIsRunning = false;
    }
}
